var timeUtils = require("./timeUtils");
var randomUtils = require("./randomUtils");

function bezier_curves(cp, t) {
  cx = 3.0 * (cp[1].x - cp[0].x);
  bx = 3.0 * (cp[2].x - cp[1].x) - cx;
  ax = cp[3].x - cp[0].x - cx - bx;
  cy = 3.0 * (cp[1].y - cp[0].y);
  by = 3.0 * (cp[2].y - cp[1].y) - cy;
  ay = cp[3].y - cp[0].y - cy - by;

  tSquared = t * t;
  tCubed = tSquared * t;
  result = {
    x: 0,
    y: 0,
  };
  result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
  result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;
  return result;
}

//仿真随机带曲线滑动
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function swipeX(qx, qy, zx, zy, time) {
  var xxy = [time];
  var point = [];
  var dx0 = {
    x: qx,
    y: qy,
  };

  var dx1 = {
    x: random(qx - 100, qx + 100),
    y: random(qy, qy + 50),
  };
  var dx2 = {
    x: random(zx - 100, zx + 100),
    y: random(zy, zy + 50),
  };
  var dx3 = {
    x: zx,
    y: zy,
  };
  point.push(dx0);
  point.push(dx1);
  point.push(dx2);
  point.push(dx3);

  for (let i = 0; i < 1; i += 0.08) {
    xxyy = [
      parseInt(bezier_curves(point, i).x),
      parseInt(bezier_curves(point, i).y),
    ];

    xxy.push(xxyy);
  }

  gesture.apply(null, xxy);
}
module.exports = {
  swipeX: swipeX,
  swipeUp() {
    var configs = [
      [
        60,
        100,
        device.width / 2,
        (device.height / 10) * 5,
        device.width / 2,
        (device.height / 10) * 2,
      ],
      [
        40,
        60,
        device.width / 2,
        (device.height / 10) * 6,
        device.width / 2,
        (device.height / 10) * 3,
      ],
      [
        30,
        40,
        device.width / 2,
        (device.height / 10) * 7,
        device.width / 2,
        (device.height / 10) * 2,
      ],
      [
        1,
        30,
        device.width / 2,
        (device.height / 10) * 8,
        device.width / 2,
        (device.height / 10) * 5,
      ],
    ];
    for (var index = 0; index < configs.length; index++) {
      var config = configs[index];
      if (randomUtils.possible(config[0], config[1])) {
        this.swipeX(
          config[2] + randomUtils.getRandom(0, 30),
          config[3] + randomUtils.getRandom(0, 30),
          config[4] + randomUtils.getRandom(0, 30),
          config[5] + randomUtils.getRandom(0, 30),
          randomUtils.getRandom(350, 800)
        );
        return;
      }
    }
  },
  swipeDown() {
    var configs = [
      [
        60,
        100,
        device.width / 3,
        device.height / 2,
        device.width / 3 + 20,
        device.height / 2 - device.height / 5,
      ],
      [
        40,
        60,
        device.width / 5,
        device.height / 3,
        device.width / 5 + 20,
        device.height / 3 - device.height / 6,
      ],
      [
        30,
        40,
        (device.width / 7) * 2,
        device.height / 4,
        (device.width / 7) * 2 + 20,
        device.height / 4 - device.height / 7,
      ],
      [
        1,
        30,
        device.width / 9,
        device.height / 5,
        device.width / 9 + 20,
        device.height / 5 - device.height / 8,
      ],
    ];
    for (var index = 0; index < configs.length; index++) {
      var config = configs[index];
      if (randomUtils.possible(config[0], config[1])) {
        this.swipeX(
          config[4] + randomUtils.getRandom(0, 30),
          config[5] + randomUtils.getRandom(0, 30),
          config[2] + randomUtils.getRandom(0, 30),
          config[3] + randomUtils.getRandom(0, 30),
          randomUtils.getRandom(200, 500)
        );
        return;
      }
    }
  },
  swipeRead(span, maxUp) {
    span = span || 60;
    span = randomUtils.getRandom(span, span + 5);
    maxUp = randomUtils.getRandom(maxUp, maxUp + 2);
    var startTime = Date.now();
    var count = 0;
    while (true) {
      count++;
      if (count < maxUp) {
        if (count % 3 == 0) {
          console.log("swipeRead:", "向上滑动一次");
        }
        this.swipeUp();
      } else {
        if (randomUtils.possible(1, 90)) {
          if (count % 3 == 0) {
            console.log("swipeRead:", "向上滑动一次");
          }
          this.swipeUp();
        } else {
          if (count % 3 == 0) {
            console.log("swipeRead:", "向下滑动一次");
          }
          this.swipeDown();
        }
      }
      sleep(randomUtils.getRandom(1.5, 3, 1000));
      var hasRead = timeUtils.hasExec(startTime);
      if (count % 5 == 0) {
        console.log("swipeRead:", hasRead, "/", span);
      }
      if (hasRead >= span) {
        if (count % 5 != 0) {
          console.log("swipeRead:", hasRead, "/", span);
        }
        break;
      }
    }
  },
  swipeVideo() {
    var configs = [
      [
        60,
        100,
        device.width / 2,
        (device.height / 10) * 7,
        device.width / 2 + 20,
        (device.height / 10) * 1 - 50,
      ],
      [
        40,
        60,
        device.width / 2,
        (device.height / 10) * 7,
        device.width / 2 + 20,
        (device.height / 10) * 1.5 - 80,
      ],
      [
        30,
        40,
        device.width / 2,
        (device.height / 10) * 8,
        device.width / 2 + 20,
        (device.height / 10) * 1.8 - 80,
      ],
      [
        1,
        30,
        device.width / 2,
        (device.height / 10) * 8.5,
        device.width / 2 + 20,
        (device.height / 10) * 2 - 80,
      ],
    ];
    for (var index = 0; index < configs.length; index++) {
      var config = configs[index];
      if (randomUtils.possible(config[0], config[1])) {
        this.swipeX(
          config[2] + randomUtils.getRandom(0, 30),
          config[3] + randomUtils.getRandom(0, 30),
          config[4] + randomUtils.getRandom(0, 30),
          config[5] + randomUtils.getRandom(0, 30),
          randomUtils.getRandom(350, 800)
        );
        return;
      }
    }
  },
};
